﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.DirectShow;
using System.Windows.Media.DirectShow.Filters;
using System.Windows.Media.DirectShow.Ogg;
using System.Windows.Media.DirectShow.UI;

namespace Demo
{
    public partial class WaveWriterPage : Page
    {

        private FilterGraph graph;
        private OggVorbisDecoder sourceFilter;
        private WaveWriter waveWriter;
        private FileWriter fileWriter;

        public WaveWriterPage()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            graph = new FilterGraph();

            sourceFilter = new OggVorbisDecoder();
            sourceFilter.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(sourceFilter_LoadCompleted);
            graph.AddFilter("source", sourceFilter);

            waveWriter = new WaveWriter();
            graph.AddFilter("wave", waveWriter);

            fileWriter = new FileWriter();
            graph.AddFilter("file", fileWriter);

            gv.SetGraph(graph, new Uri("test1.ogg", UriKind.Relative), new Uri("test2.ogg", UriKind.Relative));
        }

        void sourceFilter_LoadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                graph.ConnectFilters(sourceFilter, waveWriter, false);
                graph.ConnectFilters(waveWriter, fileWriter, false);
            }
        }

        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            base.OnNavigatingFrom(e);

            graph.StopAsync();
        }
    }
}